package com.rayframework.context.utils;

import com.ray.tool.RayStringTools;
import com.ray.tool.file.RayFileTools;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 应用配置帮助类
 *
 * @author LiaoYuXing-Ray
 * @version 1.0
 * @createDate 2023/11/26 15:20
 **/
public class ApplicationConfigUtils {


    private static String appRoot = "";

    /**
     * 工具类中的方法都是静态方法访问的，因此将构造器私有不允许创建对象
     *
     * @author LiaoYuXing-Ray 2023/9/7 10:24
     */
    private ApplicationConfigUtils() {
        throw new AssertionError();
    }

    public static String getAppRoot() {
        boolean flag = RayStringTools.isEmpty(appRoot);
        if (flag) {
            String path = System.getProperty("user.dir");
            File file = new File(path);
            path = file.getAbsolutePath();

            try {
                path = URLDecoder.decode(path, "utf-8");
            } catch (UnsupportedEncodingException var4) {
                var4.printStackTrace();
            }

            if (path.endsWith(".jar")) {
                path = path.substring(0, path.lastIndexOf(File.separator) + 1);
            }

            setAppRoot(path);
        }

        return appRoot;
    }

    public static void setAppRoot(String value) {
        if (value != null && !value.equals("")) {
            if (RayFileTools.fileExists(value)) {
                if (!value.endsWith(File.separator)) {
                    value = value + File.separator;
                }

                appRoot = value;
            }

        }
    }

}
